package com.group.activity.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.group.common.utils.SymmetricEncoder;
import com.group.common.vo.ShopUserVo;
import com.mysql.cj.util.LogUtils;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.security.crypto.bcrypt.BCrypt;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.group.activity.entity.GroupPeoplerecordEntity;
import com.group.activity.service.GroupPeoplerecordService;
import com.group.common.utils.PageUtils;
import com.group.common.utils.R;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;


/**
 * 拼团人数记录表
 *
 * @author gxx
 * @email hongyezhaofang.com
 * @date 2021-11-16 15:07:25
 */
@RestController
@RequestMapping("activity/grouppeoplerecord")
public class GroupPeoplerecordController {

    static ThreadLocal<GroupPeoplerecordEntity> threadLocal = new ThreadLocal<>();

    @Autowired
    private GroupPeoplerecordService groupPeoplerecordService;

    @Autowired
    StringRedisTemplate redisTemplate;

    @Autowired
    SymmetricEncoder symmetricEncoder;

    /*
    生成链接地址
     */
    @RequestMapping("/generatelink")
    public R generatelink(@RequestParam("groupShopId")Integer groupShopId,@RequestParam("groupUserId")Integer groupUserId){
        String aesone = symmetricEncoder.AESEncode("DES", String.valueOf(groupShopId));
        String aestwo = symmetricEncoder.AESEncode("DES", String.valueOf(groupUserId));

        redisTemplate.opsForValue().set("groupShopId", groupShopId.toString());
        redisTemplate.opsForValue().set("groupUserId", groupUserId.toString());
        redisTemplate.opsForValue().set("url", aesone+"&aestwo="+aestwo);
        return R.ok().put("data","http://localhost:88/api/activity/grouppeoplerecord/generatelinl?aesone="+aesone+"&aestwo="+aestwo);
    }

    /*
    解密地址
     */
    @RequestMapping("/generatelinl")
    public R generatelinl(@RequestParam(value = "aesone")String aesone,@RequestParam(value = "aestwo")String aestwo,HttpSession session,@RequestBody GroupPeoplerecordEntity groupPeoplerecordEntity){
/*        String one = symmetricEncoder.AESDncode("DES", aesone);
        String two = symmetricEncoder.AESDncode("DES", aestwo);
        Set<String> keys = redisTemplate.keys("group*");
        if (keys!=null){
            String groupShopId = redisTemplate.opsForValue().get("groupShopId");
            String groupUserId = redisTemplate.opsForValue().get("groupUserId");
            String url = redisTemplate.opsForValue().get("url");
            System.out.println(one);
            System.out.println(two);

            return R.ok().put("data",one+two);
        }
        return R.error();*/
        Object userss = session.getAttribute("loginUser");
        String s = JSON.toJSONString(userss);
        ShopUserVo user = JSON.parseObject(s, new TypeReference<ShopUserVo>() {
        });
        R generatelinl = groupPeoplerecordService.generatelinl(aesone,aestwo,user,groupPeoplerecordEntity);
        return R.ok("").put("data",generatelinl);

    }

/*    public String generatelinl(){

        Set<String> keys = redisTemplate.keys("group*");
        if (keys!=null){

            String url = redisTemplate.opsForValue().get("url");
            String one = symmetricEncoder.AESDncode("AES", url);
//            String two = symmetricEncoder.AESDncode("AES", aestwo);
            System.out.println(one);
//            System.out.println(two);
            return one;
        }
        return null;
    }*/




    /*
    用户加入拼团
     */
    @RequestMapping("/joinGroupbuy")
    public R joinGroupbuy(@RequestBody GroupPeoplerecordEntity groupPeoplerecordEntity, HttpSession session){
        R groupbuy = groupPeoplerecordService.joinGroupbuy(groupPeoplerecordEntity, session);

        return R.ok().put("data",groupbuy);
    }


    /*
   拼团记录列表
     */
    @RequestMapping("/groupShopList")
    public R groupShopList(@RequestParam("groupShopid")Integer groupShopid){
      List<GroupPeoplerecordEntity> groupPeoplerecordEntities=  groupPeoplerecordService.groupShopList(groupShopid);
      return R.ok("查询成功").put("data",groupPeoplerecordEntities);
    }
}
